package service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import mapper.StudentMapper;
import po.Actor;
import po.Student;
import poi.WriteExcel;
import service.StudentService;
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
@Service("studentservice")
public class StudentServiceimpl implements StudentService {
	@Autowired
	public StudentMapper studentMapper;
	@Override
	public List<Student> queryAllStduent(int pagenum, int pagesize) {
		PageHelper.startPage(pagenum, pagesize);
		List<Student> stuarr = studentMapper.queryAllStudent();
		return stuarr;
	}

	@Override
	public Student queryByid(Integer ID) {
		Student student = studentMapper.queryByid(ID);
		return student;
	}

	@Override
	public Student updateStudent(Student stu) {
	
		studentMapper.updateStudent(stu);
		return stu;
	}

	@Override
	public void deleteStudent(Integer ID) {
		studentMapper.deleteStudent(ID);
		
	}

	@Override
	public Student insertStudent(Student stu) {
		System.out.println(stu);
		studentMapper.insertStudent(stu);
		return stu;
	}

	@Override
	public List<Student> queryByname(Student stu) {
		List<Student> stuarr = studentMapper.queryByname(stu);
		return stuarr;
	}

	@Override
	public List<Student> queryByname(Student stu, Integer pagenum, Integer pagesize) {
		PageHelper.startPage(pagenum, pagesize);
		return studentMapper.queryByname(stu);
	}
	/**
	 * 获得总共多少条
	 */
	@Override
	public int getactornum() {
		List<Student> stuarr = studentMapper.queryAllStudent();
		
		return stuarr.size();
	}
	
	/**
	 * 获得数据库中所有学生信息，然后转换成字节流，写进excel
	 */
	@Override
	public InputStream getInputStream() throws Exception {
		
		String [] title = new String[] {"ID","姓名","出生日期","籍贯"};
		List<Student> plist = studentMapper.queryAllStudent();
		List<Object []> dataList = new ArrayList<Object[]> ();
		for(int i=0;i<plist.size();i++) {
			Object [] obj = new Object[4];
			obj[0]=plist.get(i).getC_id();
			obj[1]=plist.get(i).getC_name();
			obj[2]=plist.get(i).getC_birthday();
			obj[3]=plist.get(i).getC_address();
			dataList.add(obj);
		}
		WriteExcel ex = new WriteExcel(title, dataList);  
		InputStream in;
		in = ex.export();
		return in;
	}
	
	

}
